package com.sztx.pay.center.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.sztx.commons.httpclient.HttpClientConfigBuilder;
import com.sztx.commons.httpclient.HttpClientUtil;
import com.sztx.commons.httpclient.HttpHeaderBuilder;

public class GrayUsers {
	private static final Logger logger = LoggerFactory.getLogger(GrayUsers.class);	
	private static final Logger grayUserlogger = LoggerFactory.getLogger("grayUserlogger");

	public static String GRAY_USER_URL = "http://cashier.berbon.com/account/grayUser?format=json&operator=system&remark=system&userId=";
	public static String SESSION_USER_URL = "http://cms.berbon.com/support/channel/getGroupList?d=%7b%22pageNo%22%3a1%2c%22pageSize%22%3a1%7d";
		
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new FileReader("E:/文档/缴费商灰度失败列表.txt"));
		
		Set<String> unGrayUserSet = new HashSet<String>();
		List<String> unGrayUserList = FileUtils.readLines(new File("E:/文档/aaa.txt"));
		if (unGrayUserList != null && !unGrayUserList.isEmpty()) {
			for (String userId : unGrayUserList) {
				unGrayUserSet.add(userId.trim());
			}
		}
		
		
		String readLine = null;
		int i = 1;
		long lastVisitTime = 0;
		List<String> errUserIds = new ArrayList<String>();
		while ((readLine = bf.readLine()) != null) {// 使用readLine方法，一次读一行			
			long currentTime = System.currentTimeMillis();
			if (currentTime - lastVisitTime > 60000) {  //5分钟访问一次session								
				String result = getHtmlContent(SESSION_USER_URL);				
				if (result == null || !result.startsWith("{\"code\":\"J000000\"")) { //session未访问成功
					logger.info("访问session结果失败{}", result);
					System.exit(0);
				}				
				lastVisitTime = currentTime;
			}
			
			readLine = readLine.trim();
			i++;
			
			if (unGrayUserSet.contains(readLine)) {
				grayUserlogger.warn("用户{}不灰度",readLine);
				continue;
			}			
			
			String url = GRAY_USER_URL + readLine;			
			String result = getHtmlContent(url);
			String code = null;
			JSONObject parseObject = null;
			if (result != null && !result.isEmpty()) {
				parseObject = JSONObject.parseObject(result);
				code = parseObject.getString("code");
			}			
			
			String resultMsg = "【成功】";
			if ("J000997".equals(code)) {
				String errMsg = parseObject.getString("description");
				grayUserlogger.error("迁移第{}用户{}失败,失败原因{}", i, readLine, errMsg);
			}else {
				grayUserlogger.info("迁移第:" + i + "行,账号【" + readLine + "】," + resultMsg);				
			}
			
		}
		grayUserlogger.info("迁移失败用户列表:" + JSONObject.toJSONString(errUserIds));
		
	}
	
	
	private static String getHtmlContent(String url){		
		HttpClientConfigBuilder clientBuilder = HttpClientConfigBuilder.custom();
		HttpHeaderBuilder headerBuilder = HttpHeaderBuilder.custom().cookie("berbonjsessionid=089F3AFE3DB843ABB70B710CC7E784A5");		
		String encoding = "UTF-8";
		return HttpClientUtil.get(clientBuilder, url, headerBuilder, encoding);	
	}
	
	
	
}
